package com.bicon.life.core.base.repository.jpa;

import org.springframework.util.StringUtils;

import java.util.Collection;

/**
 * Created by zhoubin on 2016/1/8.
 */
public class Restrictions {
    /**
     * 为空
     * @param fieldName
     * @return
     */
    public static SimpleExpression isNull(String fieldName) {
        return new SimpleExpression (fieldName, null, Criterion.Operator.NU);
    }

    /**
     * 不为空
     * @param fieldName
     * @return
     */
    public static SimpleExpression isNotNull(String fieldName) {
        return new SimpleExpression (fieldName, null, Criterion.Operator.NNU);
    }

    /**
     * 等于
     * @param fieldName
     * @param value
     * @return
     */
    public static SimpleExpression eq(String fieldName, Object value) {
        if(StringUtils.isEmpty(value))return null;
        return new SimpleExpression (fieldName, value, Criterion.Operator.EQ);
    }

    /**
     * 关联等于
     * @param fieldName
     * @param value
     * @return
     */
    public static SimpleExpression joinEq(String fieldName, Object value, Class c, Boolean isJoin) {
        if(StringUtils.isEmpty(value))return null;
        return new SimpleExpression (fieldName, value, c, Criterion.Operator.EQ, isJoin);
    }

    /**
     * 不等于
     * @param fieldName
     * @param value
     * @return
     */
    public static SimpleExpression ne(String fieldName, Object value) {
        if(StringUtils.isEmpty(value))return null;
        return new SimpleExpression (fieldName, value, Criterion.Operator.NE);
    }

    /**
     * 模糊匹配
     * @param fieldName
     * @param value
     * @return
     */
    public static SimpleExpression like(String fieldName, String value) {
        if(StringUtils.isEmpty(value))return null;
        return new SimpleExpression (fieldName, value, Criterion.Operator.LIKE);
    }

    /**
     * 模糊匹配
     * @param fieldName
     * @param value
     * @return
     */
    public static SimpleExpression notLike(String fieldName, String value) {
        if(StringUtils.isEmpty(value))return null;
        return new SimpleExpression (fieldName, value, Criterion.Operator.NOTLIKE);
    }

    /**
     * 大于
     * @param fieldName
     * @param value
     * @return
     */
    public static SimpleExpression gt(String fieldName, Object value) {
        if(StringUtils.isEmpty(value))return null;
        return new SimpleExpression (fieldName, value, Criterion.Operator.GT);
    }

    /**
     * 小于
     * @param fieldName
     * @param value
     * @return
     */
    public static SimpleExpression lt(String fieldName, Object value) {
        if(StringUtils.isEmpty(value))return null;
        return new SimpleExpression (fieldName, value, Criterion.Operator.LT);
    }

    /**
     * 大于等于
     * @param fieldName
     * @param value
     * @return
     */
    public static SimpleExpression gte(String fieldName, Object value) {
        if(StringUtils.isEmpty(value))return null;
        return new SimpleExpression (fieldName, value, Criterion.Operator.GTE);
    }

    /**
     * 小于等于
     * @param fieldName
     * @param value
     * @return
     */
    public static SimpleExpression lte(String fieldName, Object value) {
        if(StringUtils.isEmpty(value))return null;
        return new SimpleExpression (fieldName, value, Criterion.Operator.LTE);
    }

    /**
     * 两者之间（包含边界）
     * @param fieldName
     * @param lo
     * @param go
     * @return
     */
    public static SimpleExpression between(String fieldName, Object lo, Object go) {
        if (StringUtils.isEmpty(lo) || StringUtils.isEmpty(go)) return null;
        return new SimpleExpression(fieldName, lo, go, Criterion.Operator.BETWEEN);
    }

    /**
     * 并且
     * @param criterions
     * @return
     */
    public static LogicalExpression and(Criterion... criterions){
        return new LogicalExpression(criterions, Criterion.Operator.AND);
    }

    /**
     * 或者
     * @param criterions
     * @return
     */
    public static LogicalExpression or(Criterion... criterions){
        return new LogicalExpression(criterions, Criterion.Operator.OR);
    }

    /**
     * 包含于
     * @param fieldName
     * @param value
     * @return
     */
    public static LogicalExpression in(String fieldName, Collection value) {
        if(null == value|| value.isEmpty()){
            return null;
        }
        SimpleExpression[] ses = new SimpleExpression[value.size()];
        int i=0;
        for(Object obj : value){
            ses[i]=new SimpleExpression(fieldName,obj, Criterion.Operator.EQ);
            i++;
        }
        return new LogicalExpression(ses, Criterion.Operator.OR);
    }

    /**
     * 不包含于
     * @param fieldName
     * @param value
     * @return
     */
    public static LogicalExpression notIn(String fieldName, Collection value) {
        if(null == value || value.isEmpty()){
            return null;
        }
        SimpleExpression[] ses = new SimpleExpression[value.size()];
        int i=0;
        for(Object obj : value){
            ses[i]=new SimpleExpression(fieldName,obj, Criterion.Operator.NE);
            i++;
        }
        return new LogicalExpression(ses, Criterion.Operator.OR);
    }
}